package person.xjl.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import person.xjl.domain.Student;
import person.xjl.service.StudentService;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lenovo on 2020/3/19.
 */
//注解，不被编译，定义了StudentController是一个Controller,控制器,用户请求的流转
    //当被Spring 容器扫描到时，该注解会自动生成一个Bean，Bean的名字叫studentController
@Controller
    //支持restful形式请求
//@RestController
//请求映射，定义StudentController在浏览器地址栏里的访问形式
@RequestMapping("/Student")

public class StudentController {
    //自动注入service对象studentServiceImpl
    @Autowired
    private StudentService studentServiceImpl;
    @Getter@Setter
    private List<Student> students=new ArrayList<Student>();
    //显示学生信息的方法
   // @RequestMapping("list.do")
    //支持跨域请求。跨域：8080,前端程序服务启动的端口可能是80，
    //@CrossOrigin
    @GetMapping("list.do")
//将list.do方法返回的数据渲染 前端页面的 body部分
    //@ResponseBody
    public String  list(@CookieValue("username")String username,@RequestParam(value="page",defaultValue= "1") int page, Model model){
        //测试cookie
        System.out.println(username);
        //定义PageHelper的当前页面和每页记录数
        PageHelper.startPage(page,5);
        //调用service层的list方法，获得student表里的所有学生
        students=studentServiceImpl.list();
        PageInfo pageInfo=new PageInfo(students);
        //准备把students传出去
        //model.addAttribute("students",students);
        //准备把pageInfo对象，它里面包含了很多信息：首页，上一页，总页数，总记录数，list...
        model.addAttribute("pageInfo",pageInfo);

        //转向到/webapp/WEB-INF/views/listStudents.jsp
        return "student/listStudents";
    }
    //login
    //register
    //准备访问addStudent.jsp文件
    @RequestMapping("toAdd.do")
    public String toAdd(){
        return "student/addStudent";
    }
    //add,获取并显示学生信息,request.getParameter*
    //将数据stu_name,password传送到listStudents.jsp页面显示
    @RequestMapping(value = "add.do",method = RequestMethod.POST)
    public String add(@RequestParam(value = "file") MultipartFile file, Student student, Model model) throws IOException {
//上传的动作
        uploadFile(file);
        //设置student的image属性
        student.setImage(file.getOriginalFilename());
       //调用service的add方法，添加新的学生信息到数据库表student
        studentServiceImpl.add(student);
        //转向到list.do请求,重定向redirect:
        return "redirect:list.do";

    }
    private void uploadFile(MultipartFile image) throws IOException {
        if(image!=null){
            File imageFile = new File(image.getOriginalFilename());// 上传后的文件保存目录及名字
            image.transferTo(imageFile);// 将上传文件保存到相应位置
        }
    }
    //delete student
    @RequestMapping("delete.do")
    public String delete(int id){
        studentServiceImpl.delete(id);
        return "redirect:list.do";
    }
    //准备修改学生信息
    @RequestMapping("toUpdate.do")
    public String toUpdate(int id,Model model)
    {
        Student student=studentServiceImpl.find(id);
        model.addAttribute("student",student);
        return "student/updateStudent";
    }

    //修改学生信息
    @RequestMapping("update.do")
    public String update(Student newStudent){
        studentServiceImpl.edit(newStudent);
        return "redirect:list.do";
    }
    //学生登录
    @RequestMapping("login.do")
    public String login(String name,String password){

        if(studentServiceImpl.login(name, password)==null)
        //fail
        {
            return "login";
        }
        else
            //success
        {
            return "redirect:list.do";
        }
    }
}
